package com.leilei.sensitive;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.plugin.Interceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * @author lei
 * @create 2024-05-20 16:01
 * @desc 默认开启脱敏配置
 **/
@EnableConfigurationProperties(SensitiveProperties.class)
@ConditionalOnClass(value = JsonSerialize.class)
@ConditionalOnProperty(prefix = SensitiveProperties.PREFIX, name = "enable", havingValue = "true", matchIfMissing = true)
@Slf4j
public class SensitiveAutoConfiguration {
    public static Boolean sensitiveEnable = false;
    public static String maskStr = "*";
    public static Boolean printSensitiveLog = false;
    public static Boolean printReverseLog = false;

    public SensitiveAutoConfiguration(SensitiveProperties knife4jProperties) {
        sensitiveEnable = knife4jProperties.getEnable();
        maskStr = knife4jProperties.getMaskStr();
        printReverseLog = knife4jProperties.getPrintReverseLog();
        printSensitiveLog = knife4jProperties.getPrintSensitiveLog();
        if (sensitiveEnable) {
            log.info("开启数据脱敏功能,脱敏标识:{}", maskStr);
        }
    }

    /**
     * mp拦截器解放双手型逆脱敏
     * @author lei
     * @date 2025-05-23 10:41:20
     * @param applicationContext
     * @return Interceptor
     */
    @Bean
    @ConditionalOnProperty(prefix = SensitiveProperties.PREFIX, name = "reverse-mp", havingValue = "true", matchIfMissing = false)
    @ConditionalOnClass(value = Interceptor.class)
    public Interceptor reverseSensitiveInterceptor(ApplicationContext applicationContext) {
        log.info("开启mp数据源逆脱敏功能");
        ReverseSensitiveMpInterceptor reverseSensitiveMpInterceptor = new ReverseSensitiveMpInterceptor();
        reverseSensitiveMpInterceptor.setApplicationContext(applicationContext);
        return reverseSensitiveMpInterceptor;
    }

}
